package com.example.controller;

import com.example.domain.Girl;
import com.example.domain.Result;
import com.example.properties.GirlPropertity;
import com.example.repository.GirlRepository;
import com.example.service.GirlService;
import com.example.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by skyr on 2019/1/24.
 */
@RestController
public class GirlController {

    private final static Logger logger = LoggerFactory.getLogger(GirlController.class);

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlPropertity girlPropertity;

    @Autowired
    private GirlService girlService;

    /**
     * 增加一个女孩
     * PostMan post提交
     * 示例：http://localhost:8082/girl/add?cupSize=A&age=18&money=1
     * @param girl
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/girl/add")
    public Result<Girl> girlAdd(@Valid Girl girl, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
        }

        girl.setCupSize(girl.getCupSize());
        girl.setAge(girl.getAge());
        girl.setMoney(girl.getMoney());

        logger.info("girl=" + girl);
        return ResultUtil.success(girlRepository.save(girl));
    }

    @GetMapping(value = "girls/getAge/{id}")
    public void getAge(@PathVariable("id") Integer id) throws Exception{
        girlService.getAge(id);
    }

}

